<?php
require_once ("fct_entete.php");
require_once ("fct_config.inc.php");

//connexion à la base
$link = mysql_connect(HOST,USER,PASS);
mysql_select_db(BASE, $link);

//parametre graphiques
$largeurGraph=525;
$hauteurGraph=375;
$placeGraph=0.32;
$tailleGraph=0.32;
$pourcent="%%";

function creerGraph($query, $nomGraph) {
	if(file_exists($nomGraph)) {
		unlink($nomGraph);
	}

	//parametre graphiques
	$largeurGraph=525;
	$hauteurGraph=375;
	$placeGraph=0.32;
	$tailleGraph=0.32;
	$pourcent="%%";

	$tableauLibelle = array();
	$tableauVal = array();

	$nbLignes=0;

	$result = mysql_query($query);
	while ($donnees = mysql_fetch_array($result)){
		$tableauLibelle[] = $donnees[1]." ".$donnees[0];
		$tableauVal[] = $donnees[0];
		$nblignes=$nbLignes+1;
	}

	if($nblignes>0){
		// **************************************
		// Création du graphique
		// *****************************************
		// On spécifie la largeur et la hauteur du graph
		$graph = new PieGraph($largeurGraph, $hauteurGraph);

		// Ajouter une ombre au conteneur
		$graph->SetColor("gray");

		// Créer un camembert
		$pie = new PiePlot($tableauVal);

		// Légendes qui accompagnent le graphique, ici chaque année avec sa couleur
		$pie->SetLegends($tableauLibelle);

		// Position et taille du graphique (0.5=centré)
		$pie->SetCenter($placeGraph);
		$pie->SetSize($tailleGraph);

		// Type de valeur (pourcentage ou valeurs)
		$pie->SetValueType(PIE_PROCENT);
		// Personnalisation des étiquettes pour chaque partie
		$pie->value->SetFormat('%d'.$pourcent);

		//$pie->SetTheme("earth");
		$pie->SetSliceColors(array('bisque4','wheat1','aquamarine3','blue','cadetblue3',
							'chartreuse3','chocolate2','cyan3','cyan4','darkmagenta',
							'darkolivegreen2','dodgerblue3','dodgerblue4','goldenrod',
							'gray2','khaki1','lightsalmon','lightskyblue3','steelblue4','yellow',
							'brown','gray8','cora14','firebrick'));

		// ajouter le graphique PIE3D au conteneur
		$graph->Add($pie);

		// Provoquer l'affichage
		$graph->Stroke($nomGraph);
	}
}



function lastdaymonth($month,$year)
{
	$lastday = date("j", mktime(0,0,0, $month + 1, 0, $year));
	return $lastday;
}


function conversionDate($date)
{
	switch ($date){
		case 1:
			$nomMois="Janvier";
			break;
		case 2:
			$nomMois="Février";
			break;
		case 3:
			$nomMois="Mars";
			break;
		case 4:
			$nomMois="Avril";
			break;
		case 5:
			$nomMois="Mai";
			break;
		case 6:
			$nomMois="Juin";
			break;
		case 7:
			$nomMois="Juillet";
			break;
		case 8:
			$nomMois="Août";
			break;
		case 9:
			$nomMois="Septembre";
			break;
		case 10:
			$nomMois="Octobre";
			break;
		case 11:
			$nomMois="Novembre";
			break;
		case 12:
			$nomMois="Décembre";
			break;
	}
	return $nomMois;
}

function dateInverseSortie($date)
{
	//renvoie true sur la date est valide sinon false
	if(ereg("^([0-9]{4})[-/]([0-1][0-9])[-/]([0-3][0-9])$",$date,$reg))
	{
		$_GET['date']=$reg[3]."/".$reg[2]."/".$reg[1];
		if(checkdate($reg[2],$reg[3],$reg[1]))
		{
			return $_GET['date'];
		}
		else
		{
			return "erreur";
		}
	}
	else
	{
		return "erreur1";
	}
}

function dateInverseEntree($date)
{
	//renvoie true sur la date est valide sinon false
	if(ereg("^([0-3][0-9])[-/]([0-1][0-9])[-/]([0-9]{4})$",$date,$reg))
	{
		$_GET['date']=$reg[3]."-".$reg[2]."-".$reg[1];
		if(checkdate($reg[2],$reg[1],$reg[3]))
		{
			return $_GET['date'];
		}
		else
		{
			return "erreur";
		}
	}
	else
	{
		return "erreur1";
	}
}

function listeMois($param){
	echo "<select name='mois' 'value='".$param."'> ";
	echo "<option selected='selected' value='01'>Janvier</option>\n";
	echo "<option value='02'>Février</option>\n";
	echo "<option value='03'>Mars</option>\n";
	echo "<option value='04'>Avril</option>\n";
	echo "<option value='05'>Mai</option>\n";
	echo "<option value='06'>Juin</option>\n";
	echo "<option value='07'>Juillet</option>\n";
	echo "<option value='08'>Aout</option>\n";
	echo "<option value='09'>Septembre</option>\n";
	echo "<option value='10'>Octobre</option>\n";
	echo "<option value='11'>Novembre</option>\n";
	echo "<option value='12'>Décembre</option>\n";
	echo "<option value='13'>Année</option>\n";
	echo "</select>";
}


function listeEtat($param){
	echo "<select name='etat' 'value='".$param."'> ";
	$query = 'select * from ETAT ORDER BY libelleEtat;';
	$result = mysql_query($query);
	while($val=mysql_fetch_array($result)){
		if($val["noEtat"]==$param){
			echo "<option selected='selected' value=".$val["noEtat"].">".$val["libelleEtat"]."</option>\n";
		}
		else{
			echo "<option value=".$val["noEtat"].">".$val["libelleEtat"]."</option>\n";
		}
	}
	echo "</select>";
}

function listeSituation($param){
	echo "<select name='situation' 'value='".$param."'> ";
	$query = 'select * from SITUATION ORDER BY SITUATION.libelleSituation;';
	$result = mysql_query($query);
	while($val=mysql_fetch_array($result)){
		if($val["noSituation"]==$param){
			echo "<option selected='selected' value=".$val["noSituation"].">".$val["libelleSituation"]."</option>\n";
		}
		else{
			echo "<option value=".$val["noSituation"].">".$val["libelleSituation"]."</option>\n";
		}
	}
	echo "</select>";
}

function listeAge($param){
	echo "<select name='age' 'value='".$param."'> ";
	$query = 'select * from AGE ORDER BY AGE.libelleAge;';
	$result = mysql_query($query);
	while($val=mysql_fetch_array($result)){
		if($val["noAge"]==$param){
			echo "<option selected='selected' value=".$val["noAge"].">".$val["libelleAge"]."</option>\n";
		}
		else{
			echo "<option value=".$val["noAge"].">".$val["libelleAge"]."</option>\n";
		}
	}
	echo "</select>";
}

function listeCivilite($param){
	echo "<select name='civilite' 'value='".$param."'> ";
	$query = 'select * from CIVILITE ORDER BY CIVILITE.libelleCivilite;';
	$result = mysql_query($query);
	while($val=mysql_fetch_array($result)){
		if($val["noCivilite"]==$param){
			echo "<option selected='selected' value=".$val["noCivilite"].">".$val["libelleCivilite"]."</option>\n";
		}
		else{
			echo "<option value=".$val["noCivilite"].">".$val["libelleCivilite"]."</option>\n";
		}
	}
	echo "</select>";
}

function listeRessource($param){
	echo "<select name='ressource' 'value='".$param."'> ";
	$query = 'select * from RESSOURCE ORDER BY RESSOURCE.libelleRessource;';
	$result = mysql_query($query);
	while($val=mysql_fetch_array($result)){
		if($val["noRessource"]==$param){
			echo "<option selected='selected' value=".$val["noRessource"].">".$val["libelleRessource"]."</option>\n";
		}
		else{
			echo "<option value=".$val["noRessource"].">".$val["libelleRessource"]."</option>\n";
		}
	}
	echo "</select>";
}

function listeLogement($param){
	echo "<select name='logement' 'value='".$param."'> ";
	$query = 'select * from LOGEMENT ORDER BY LOGEMENT.libelleLogement;';
	$result = mysql_query($query);
	while($val=mysql_fetch_array($result)){
		if($val["noLogement"]==$param){
			echo "<option selected='selected' value=".$val["noLogement"].">".$val["libelleLogement"]."</option>\n";
		}
		else{
			echo "<option value=".$val["noLogement"].">".$val["libelleLogement"]."</option>\n";
		}
	}
	echo "</select>";
}

function listeOrganisme($param){
	echo "<select name='organisme' 'value='".$param."'> ";
	$query = 'select * from ORGANISME ORDER BY ORGANISME.libelleOrganisme;';
	$result = mysql_query($query);
	while($val=mysql_fetch_array($result)){
		if($val["noOrganisme"]==$param){
			echo "<option selected='selected' value=".$val["noOrganisme"].">".$val["libelleOrganisme"]."</option>\n";
		}
		else{
			echo "<option value=".$val["noOrganisme"].">".$val["libelleOrganisme"]."</option>\n";
		}
	}
	echo "</select>";
}

function listeParticipant($param){
	echo "<select name='participant' 'value='".$param."'> ";
	$query = 'select * from FAMILLE ORDER BY FAMILLE.nom';
	$result = mysql_query($query);
	while($val=mysql_fetch_array($result)){
		$bene="";
		if($val["beneDistribution"]){
			$bene="      BENEFICIAIRE";
		}

		if($val["noFamille"]==$param){
			echo "<option value=".$val["noFamille"].">".$val["nom"]." ".$val["prenom"]." -- ".$bene."</option>\n";
		}
		else{
			echo "<option value=".$val["noFamille"].">".$val["nom"]." ".$val["prenom"]." -- ".$bene."</option>\n";
		}
	}
	echo "</select>";
}

function listeFamilleBene($param){
	echo "<select name='famille' 'value='".$param."'> ";
	$query = 'select * from FAMILLE WHERE beneDistribution=1 ORDER BY FAMILLE.nom';
	$result = mysql_query($query);
	while($val=mysql_fetch_array($result)){
		if($val["noOrganisme"]==$param){
			echo "<option value=".$val["noFamille"].">".$val["nom"]." ".$val["prenom"]."</option>\n";
		}
		else{
			echo "<option value=".$val["noFamille"].">".$val["nom"]." ".$val["nom"]."</option>\n";
		}
	}
	echo "</select>";
}



function selectOuiNon($attribut, $param)
{
	if($param=="1")
	{
		echo "
			    <input type=\"radio\" name=\"".$attribut."\" value=\"1\" checked='checked' /><label>Oui</label>
			    <input type=\"radio\" name=\"".$attribut."\" value=\"0\" /><label>Non</label>";
	}
	/*else if($param=="0")
	 {
	 echo "
	 <input type=\"radio\" name=\"".$attribut."\" value=\"1\"  /><label>Oui</label>
	 <input type=\"radio\" name=\"".$attribut."\" value=\"0\" checked='checked' /><label>Non</label>";
	 }*/
	else
	{
		echo "
			    <input type=\"radio\" name=\"".$attribut."\" value=\"1\"  /><label>Oui</label>
			    <input type=\"radio\" name=\"".$attribut."\" value=\"0\" checked='checked' /><label>Non</label>";
	}
}

function verifDate($date)
{
	//renvoie true sur la date est valide sinon false
	if(ereg("^([0-3][0-9])[-/]([0-1][0-9])[-/]([0-9]{4})$",$date,$reg))
	{
		$_GET['date']=$reg[3]."-".$reg[2]."-".$reg[1];
		if(checkdate($reg[2],$reg[1],$reg[3]))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	else
	{
		return false;
	}
}


function verifDateSortie($date)
{
	//renvoie true sur la date est valide sinon false
	if(ereg("^([0-3][0-9])[-/]([0-1][0-9])[-/]([0-9]{4})$",$date,$reg))
	{
		return true;
	}
	else
	{
		return false;
	}
}

?>
